import 'package:badges/badges.dart' as badges;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/page/train/train_tab_details1_page.dart';
import 'package:flutter_learn_vi/src/utils/log_util.dart';
import 'package:get/get.dart';

import '../../bean/bean_train.dart';
import '../../utils/toast_utils.dart';
import '../common/common_loading_dialog.dart';
import '../trainnew/train_line_course_details_page.dart';
import 'controller/train_notification_recoder_controller.dart';

///用户的消息列表
class TrainNotificationRecoderPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _TrainNotificationRecoderPageState();
  }
}

class _TrainNotificationRecoderPageState
    extends State<TrainNotificationRecoderPage>
    with SingleTickerProviderStateMixin {
  TrainNotificationRecoderController _checkRecoderController;

  @override
  void initState() {
    super.initState();
    _checkRecoderController = Get.put(TrainNotificationRecoderController());

    Future.delayed(Duration.zero, () async {
      await _checkRecoderController.requestUserCheckRecoderFunction();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("消息"),
      ),
      body: SizedBox.expand(
        child: GetBuilder(
          id: "root",
          builder: (TrainNotificationRecoderController controller) {
            return buildAnimatedSwitcher(context);
          },
        ),
      ),
    );
  }

  AnimatedSwitcher buildAnimatedSwitcher(BuildContext context) {
    return AnimatedSwitcher(

        //动画执行切换时间
        duration: const Duration(milliseconds: 200),
        //动画构建器 构建指定动画类型
        transitionBuilder: (Widget child, Animation<double> animation) {
          //执行缩放动画
          return FadeTransition(child: child, opacity: animation);
        },
        //执行动画的子 Widget
        //只有子 Widget 被切换时才会触发动画
        child: buildMyScoreHisIntegralWidget());
  }

  buildMyScoreHisIntegralWidget() {
    bool _isLoading = _checkRecoderController.getIsLoading();
    if (_isLoading) {
      return Center(
        child: LoadingWidget(),
      );
    }

    return RefreshIndicator(
      onRefresh: () async {
        await _checkRecoderController.requestUserCheckRecoderFunction();
        ToastUtils.showToast("已刷新");
        return true;
      },
      child: buildListView(),
    );
  }

  buildListView() {
    List<TrainNotificationRecoderEntity> list =
        _checkRecoderController.getlList();
    if (list.length == 0) {
      return Center(
        child: NoDataWidget(
          clickCallBack: () async {
            await _checkRecoderController.requestUserCheckRecoderFunction(
                click: true);
          },
        ),
      );
    }
    return ListView.builder(
      padding: EdgeInsets.zero,
      itemCount: list.length,
      itemBuilder: (_, index) {
        TrainNotificationRecoderEntity model = list[index];
        return GestureDetector(
          onTap: () {
            LogUtils.e("点击 ..");
            String trainLine = model.trainLine;
            if (trainLine == "online") {
              Get.to(
                () => TrainLineCourseDetailsPage(
                  trainId: model.trainId,
                ),
              );
            } else {
              Get.to(
                () => TrainTabDetails1Page(
                  approveBean: TrainApproveBean(model.trainId, ""),
                ),
              );
            }

            model.statues = 1;
            _checkRecoderController.readMessage(model.id);
          },
          child: _buildItem(model),
        );
      },
    );
  }

  Widget _buildItem(TrainNotificationRecoderEntity model) {
    TextStyle titleStyle = const TextStyle(
      fontSize: 18.0,
      color: Color(0xffc333333),
      fontWeight: FontWeight.bold,
    );
    TextStyle contentStyle = const TextStyle(
      fontSize: 14.0,
      color: Color(0xffc333333),
    );
    int statues = model.statues;
    bool isRead = false;
    if (statues == 1) {
      isRead = true;
    }
    return Padding(
      padding: const EdgeInsets.only(left: 8.0, top: 8.0, right: 8.0),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(4.0),
        child: Material(
          color: Colors.white,
          child: Padding(
            padding:
                const EdgeInsets.symmetric(horizontal: 8.0, vertical: 15.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    Expanded(
                      child: Text(
                        model.messageTitle,
                        style: titleStyle,
                      ),
                    ),
                    isRead
                        ? Text(
                            "已读",
                            style: TextStyle(color: Colors.blue),
                          )
                        : badges.Badge(
                            child: Text("未读"),
                            position: badges.BadgePosition.topStart(
                                top: -8, start: 24),
                          )
                  ],
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 12.0, bottom: 6.0),
                  child: Text(
                    model.messageContent ?? "",
                    style: contentStyle,
                  ),
                ),
                Text(
                  "${model.createTime}",
                  style: contentStyle,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
